
import sys
import os
import re
import copy

FILENAME_ZIGE = "zige";
FILENAME_JINPAI = "jinpai";

JINPAI_MAPPING = {
				20747570:1, 
				20747571:2, 
				20747572:3, 
				20747573:4, 
				20747574:5
				};
				
CHUANG_MAPPING = {
				1:20747610,
				2:20747609,
				3:20747608,
				4:20747607,
				5:20747606};


class CRoleInfo:
	def __init__(self):
		self.m_iZoneId = 0;
		self.m_iUin = 0
		self.m_ullGid = 0;
		self.m_listZige = [];
		self.m_listJinpai = [];

class CFinanceParse:
	def __init__(self):
		self.m_dictZige = {};
		self.m_dictJinPai = {};
		self.m_dictResult = {};
		
	def ParseZige(self):
		fp = open(FILENAME_ZIGE,'r');
		for eachline in fp:
			szPatter = '(\d+)\s+(\d+)\s+(\d+)\s+(\d+)';
			m = re.match(szPatter, eachline);
			if m is not None and len(m.groups()) == 4:
				iZoneId = (int)(m.groups()[0]);
				iZige = (int)(m.groups()[1]);
				iUin = (int)(m.groups()[2]);
				ullKey = (int)(m.groups()[3]);
				
				if ullKey not in self.m_dictZige: 
					cRoleInfo = CRoleInfo();
					cRoleInfo.m_iZoneId = iZoneId;
					cRoleInfo.m_listZige.append(iZige);
					cRoleInfo.m_iUin = iUin;
					cRoleInfo.m_ullGid = ullKey; 
					self.m_dictZige[ullKey] = cRoleInfo;
				else:
					self.m_dictZige[ullKey].m_listZige.append(iZige);
		fp.close();
	def ParseJinpai(self):
		fp = open(FILENAME_JINPAI,'r');
		for eachline in fp:
			szPatter = '(\d+)\s+(\d+)\s+(\d+)\s+(\d+)';
			m = re.match(szPatter, eachline);
			if m is not None and len(m.groups()) == 4:
				iZoneId = (int)(m.groups()[0]);
				iResId = (int)(m.groups()[1]);
				iUin = (int)(m.groups()[2]);
				ullKey = (int)(m.groups()[3]);
				if iResId not in JINPAI_MAPPING:
					print("error");
					assert(False);
					return;
				iZigeReal = JINPAI_MAPPING[iResId];
					
				
				if ullKey not in self.m_dictJinPai: 
					cRoleInfo = CRoleInfo();
					cRoleInfo.m_iZoneId = iZoneId;
					cRoleInfo.m_iUin = iUin;
					cRoleInfo.m_ullGid = ullKey; 
					cRoleInfo.m_listJinpai.append(iZigeReal);
					self.m_dictJinPai[ullKey] = cRoleInfo;
				else:
					if iZigeReal not in self.m_dictJinPai[ullKey].m_listJinpai:
						self.m_dictJinPai[ullKey].m_listJinpai.append(iZigeReal);
				
		fp.close();
	def ParseRelation(self):
		self.m_dictResult = copy.deepcopy(self.m_dictJinPai);
		for keyResult in self.m_dictResult.keys():
			if keyResult in self.m_dictZige:
				cRoleInfoZige = self.m_dictZige[keyResult];
				cRoleInfoJinpai = self.m_dictResult[keyResult];
				
				for iZige in cRoleInfoZige.m_listZige:
					if iZige in cRoleInfoJinpai.m_listJinpai:
						iIndex = cRoleInfoJinpai.m_listJinpai.index(iZige);
						cRoleInfoJinpai.m_listJinpai[iIndex] = -1;
				
			

	def GenerateOutPut(self):
		print "iZoneId\tiUin\tiGid\tiResID";
		for keyResult in self.m_dictResult.keys():
			cRoleInfoJinpai = self.m_dictResult[keyResult];
			strResIDList = "";
			for key in cRoleInfoJinpai.m_listJinpai:
				if key != -1:
					iResId = CHUANG_MAPPING[key];
					print "%10d\t%10d\t%10d\t%d" % (cRoleInfoJinpai.m_iZoneId, cRoleInfoJinpai.m_iUin, cRoleInfoJinpai.m_ullGid,iResId);		
	


def main():
	cFinanceParse = CFinanceParse();
	cFinanceParse.ParseZige(); 	
	cFinanceParse.ParseJinpai();
	cFinanceParse.ParseRelation();
	cFinanceParse.GenerateOutPut();
	

if __name__ == '__main__':
	main();

